package com.octane.flipImage;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;

public class CanvasClass extends View
{
	Midlet mMidlet;
	Bitmap bmp;
//	Bitmap bmpFlip;
	public CanvasClass(Context context)
	{
		super(context);
		this.mMidlet = (Midlet)context;
		bmp = LoadImageFunction(context,R.drawable.jet);
//		Matrix m = new Matrix();
//		m.preScale(-1.0f,1.0f);
//		bmpFlip = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, false); 
		
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Paint p = new Paint();
		p.setColor(Color.WHITE);
		canvas.drawRect(0, 0, 320, 430, p);
		p.setColor(Color.BLACK);
		p.setTextAlign(Align.CENTER);
		
		canvas.drawText("Original", 50, 20, p);
		canvas.drawBitmap(bmp, 10, 10 , p);
		
		canvas.drawText("Flip Horizontal", 50, 20 + bmp.getHeight(), p);
		Matrix mMatrixflip = new Matrix();
		mMatrixflip.setTranslate(10 + bmp.getWidth(), 10 + bmp.getHeight());
		mMatrixflip.preScale(-1.0f, 1.0f);
		canvas.drawBitmap(bmp, mMatrixflip, p);
		
		canvas.drawText("Flip Vertical", 50, 20 + ( 2 * bmp.getHeight()), p);
		mMatrixflip.setTranslate(10, 10 + ( 2 * bmp.getHeight()) + bmp.getHeight());
		mMatrixflip.preScale(1.0f, -1.0f);
		canvas.drawBitmap(bmp, mMatrixflip, p);
	}
	
	public Bitmap LoadImageFunction(Context context,int Resource)
	{
		Bitmap Image = null;
		try
		{
			InputStream is = context.getResources().openRawResource(Resource);
			Image = BitmapFactory.decodeStream(is);
			is.close();
		}catch(Exception e){}
		return Image;
	}
	
}
